package lucene;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class Indexa {
	static COSDocument cosDoc = null; 
	   static PDDocument pdDoc = null;
	   static PDDocument pddDocument = null;
	   
		
		  public  Indexa () {
			  try{
				File indexDir = new File("c:\\indexdir");
				File dataDir = new File("d:\\temp2");
				
			    
			    
			    long start = new Date().getTime();
			    int numIndexed = index(indexDir, dataDir);
			    long end = new Date().getTime();
			    System.out.println("Se han indizado " + numIndexed + " archivos          en " + (end - start) + " milisegundos");
	            
			  }
			  catch(Exception err){
				  System.out.println("Error 1: "+err);
			  }
			    
			  }
			  public static int index(File indexDir, File dataDir)
			    throws IOException {
				  if (!dataDir.exists() || !dataDir.isDirectory()) {
				      throw new IOException(dataDir
				        + " no existe o no es un directorio ");
				    }
				  Directory directory = FSDirectory.open(indexDir);
				  int numIndexed = 0;
				  try{
				    
				   borrarDirectorio(indexDir);
				  IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED);
				    writer.setUseCompoundFile(false);
				    indexDirectory(writer, dataDir);
				    numIndexed = writer.numDocs();
				    writer.optimize();
	                directory.close();
	                writer.close();
				  }
				  catch(Exception err){
					  System.out.println("Errorrrrrrr!!"+err);
					  directory.close();
				  }
				    return numIndexed;
			  }
			  private static void indexDirectory(IndexWriter writer, File dir)
			    throws IOException {
			    File[] files = dir.listFiles();
			    for (int i = 0; i < files.length; i++) {
			      File f = files[i];
			      if (f.isDirectory()) {
			        indexDirectory(writer, f);  // recurse
			      } else{
			        indexFile(writer, f);
			      }
			    }
			  }
			  
			  private static void borrarDirectorio(File dir){
				  try{
				  File[] ficheros = dir.listFiles();
				  for (int x=0;x<ficheros.length;x++){
					  if (ficheros[x].isDirectory()) {
						  borrarDirectorio(ficheros[x]);
						}
					  System.out.print("Borrando: "+ficheros[x]);
					  ficheros[x].delete();
				  }
				  }catch(Exception err){
					  
				  }
			  }
			  
				private static COSDocument parseDocument(InputStream is)
				throws IOException {
				PDFParser parser = new PDFParser(is);
				parser.parse();
				return parser.getDocument();
				}
			  
			  private static void indexFile(IndexWriter writer, File f) throws IOException {
			     if (f.getName().endsWith(".pdf")){
				  try{
					  if (f.isHidden() || !f.exists() || !f.canRead()) {
					      return;
					    }
					    System.out.println("Indizando " + f.getCanonicalPath());
					    Document doc = new Document();
					    doc.add(new Field("contents", new FileReader(f)));
					    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
					    
					    cosDoc = parseDocument(new FileInputStream(f)); 
					    if (!cosDoc.isEncrypted()){
						    pdDoc = new PDDocument(cosDoc); 
						    PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
						  
						    //contenido
						    
						    pddDocument=PDDocument.load(f);
						    PDFTextStripper textStripper=new PDFTextStripper();
						    String text = textStripper.getText(pddDocument);
						    
						    doc.add(new Field("Contenido",text,Field.Store.YES, Field.Index.ANALYZED));
						    pddDocument.close();
					    
						    if (docInfo.getAuthor() == null){
						       doc.add(new Field("Autor","Sin Autor",Field.Store.YES, Field.Index.ANALYZED));
						    }
						    else{
						    	doc.add(new Field("Autor", docInfo.getAuthor(), Field.Store.YES, Field.Index.ANALYZED));
						    }
						    if (docInfo.getCreationDate().toString() != null){
						        
						    	doc.add(new Field("Fecha",docInfo.getCreationDate().getTime().toLocaleString().substring(0,10),Field.Store.YES, Field.Index.ANALYZED));
						    	System.out.println(docInfo.getCreationDate().getTime().toLocaleString().substring(0,10));
			
						    }
						    else{
						    	doc.add(new Field("Fecha", "Sin fecha", Field.Store.YES, Field.Index.ANALYZED));
						    }
						    doc.add(new Field("Tipo", "pdf", Field.Store.YES, Field.Index.ANALYZED));
						    cosDoc.close();
						    pdDoc.close();
							    
						    writer.addDocument(doc);
						    writer.optimize();
						    writer.commit();
					    }
					    else{
					    	cosDoc.close();
					    	System.out.println("PDF con password");

					    }
				  }
				    catch(Exception err){
					    cosDoc.close();
					    pdDoc.close();
					    pddDocument.close();			    	
				    	System.out.println("Error!!!: "+err);
				    }
				  }//fin pdf
			     
			     if (f.getName().endsWith(".doc")){
			    	 try{
			 		    if (f.isHidden() || !f.exists() || !f.canRead()) {
			 		      return;
			 		    }
			 		    System.out.println("Indizando " + f.getCanonicalPath());
			 		   Document doc = new Document();
			 		    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
			 		    
			 		    WordExtractor we = new WordExtractor(new FileInputStream(f));
			 		    doc.add(new Field("Contenido", we.getText(),Field.Store.YES, Field.Index.ANALYZED ));
			 		    if (we.getSummaryInformation().getAuthor() == null){
			 			       doc.add(new Field("Autor","Sin Autor", Field.Store.YES, Field.Index.ANALYZED));
			 			}
			 			else{
			 			        doc.add(new Field("Autor", we.getSummaryInformation().getAuthor(), Field.Store.YES, Field.Index.ANALYZED) );
			 			}
			 		    if (we.getSummaryInformation().getCreateDateTime().toLocaleString().substring(0,10) == null){
			 			       doc.add(new Field("Fecha","Sin fecha", Field.Store.YES, Field.Index.ANALYZED));
			 			}
			 			else{
			 				doc.add(new Field("Fecha", we.getSummaryInformation().getCreateDateTime().toLocaleString().substring(0,10), Field.Store.YES, Field.Index.ANALYZED) );
			 			}
					    doc.add(new Field("Tipo", "doc", Field.Store.YES, Field.Index.ANALYZED));
			 		    System.out.println(we.getSummaryInformation().getAuthor());
			 		    writer.addDocument(doc);
			 		    writer.optimize();
			 		    writer.commit();
			 	  }
			 	  catch(Exception err){
			 		  System.out.println("Error: "+err);
			 	  }

			     }//fin doc
			     
			     if (f.getName().endsWith(".ppt")){
			    	 try{
			    		    
			  		   if (f.isHidden() || !f.exists() || !f.canRead()) {
			  		      return;
			  		    }
			  		    System.out.println("Indizando " + f.getCanonicalPath());
			  		  Document doc = new Document();
			  		    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
			  		    
			  		    PowerPointExtractor pw = new PowerPointExtractor(new FileInputStream(f));
			  		    doc.add(new Field("Contenido", pw.getText(),Field.Store.YES, Field.Index.ANALYZED ));
			  		    if (pw.getSummaryInformation().getAuthor() == null){
			  			       doc.add(new Field("Autor","Sin Autor", Field.Store.YES, Field.Index.ANALYZED));
			  			}
			  			else{
			  			        doc.add(new Field("Autor", pw.getSummaryInformation().getAuthor(), Field.Store.YES, Field.Index.ANALYZED) );
			  			}
			  		    if (pw.getSummaryInformation().getCreateDateTime().toLocaleString().substring(0,10) == null){
			  			       doc.add(new Field("Fecha","Sin fecha", Field.Store.YES, Field.Index.ANALYZED));
			  			}
			  			else{
			  				doc.add(new Field("Fecha", pw.getSummaryInformation().getCreateDateTime().toLocaleString().substring(0,10), Field.Store.YES, Field.Index.ANALYZED) );
			  			}
					    doc.add(new Field("Tipo", "ppt", Field.Store.YES, Field.Index.ANALYZED));
			  		    
			  		    System.out.println(pw.getSummaryInformation().getAuthor());
			  		    System.out.println(pw.getSummaryInformation().getCreateDateTime().toLocaleString().substring(0,10));
			  		    writer.addDocument(doc);
			  		    writer.optimize();
			  		    writer.commit();
			  	  }
			  	  catch(Exception err){
			  		  System.out.println("Erro: "+err);
			  	  } 
			     }
			     
			  }
}
